package com.platform.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.platform.annotation.SysLog;
import com.platform.common.ServerResponse;
import com.platform.dao.PermissionMapper;
import com.platform.pojo.Permission;
import com.platform.service.IPermissionService;
import com.platform.util.NumberUtil;
import com.platform.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service("iPermissionService")
public class PermissionServiceImpl implements IPermissionService {

    @Autowired
    private PermissionMapper permissionMapper;


    @SysLog("新增操作权限")
   /* @Caching(evict = {
            @CacheEvict(value = "selectByRoleCode",allEntries = true)
    })*/
    public ServerResponse<String> insert(Map paramMap){
        Permission permission = new Permission();
        permission.setName(StringUtil.valueOfString(paramMap.get("name")));
        permission.setUrl(StringUtil.valueOfString(paramMap.get("url")));
        Integer countResult = permissionMapper.insert(permission);
        if(countResult > 0){
            return ServerResponse.createBySuccessMessage("保存成功");
        }
        return ServerResponse.createByErrorMessage("保存失败");
    }

    @SysLog("删除操作权限")
    /*@Caching(evict = {
            @CacheEvict(value = "selectByRoleCode",allEntries = true)
    })*/
    public ServerResponse<String> del(int id){
        Integer countResult = permissionMapper.deleteByPrimaryKey(id);
        if(countResult > 0){
            return ServerResponse.createBySuccessMessage("操作成功");
        }
        return ServerResponse.createByErrorMessage("操作失败");
    }

    @SysLog("更新操作权限")
    /*@Caching(evict = {
            @CacheEvict(value = "selectByRoleCode",allEntries = true)
    })*/
    public ServerResponse<Permission> update(Map paramMap){
        Permission permission = permissionMapper.selectByPrimaryKey(NumberUtil.safeToNumber(paramMap.get("id")));
        if(null == permission){
            return ServerResponse.createByErrorMessage("数据错误");
        }
        permission.setUrl(StringUtil.valueOfString(paramMap.get("url")));
        permission.setName(StringUtil.valueOfString(paramMap.get("name")));
        Integer countResult = permissionMapper.updateByPrimaryKeySelective(permission);
        if(countResult > 0){
            return ServerResponse.createBySuccess("更新成功",permission);
        }
        return ServerResponse.createByErrorMessage("更新失败");
    }

    /**
     * 根据角色查询操作权限
     * @param param
     * @return
     */
    @Override
    @SysLog("查询操作权限")
    /*@Cacheable("selectByRoleCode")*/
    public ServerResponse<PageInfo> selectByRoleCode(Map param) {
        PageHelper.startPage(NumberUtil.safeToNumber(param.get("pageNo"),1),
                NumberUtil.safeToNumber(param.get("pageSize"),10),StringUtil.valueOfString(param.get("orderBy")));
        List<Permission> permissionList = permissionMapper.selectByRoleCode(param);
        PageInfo pageInfo = new PageInfo(permissionList);
        pageInfo.setList(permissionList);
        return ServerResponse.createBySuccess(pageInfo);
    }

    /**
     * 根据条件查询操作权限
     * @param param
     * @return
     */
    public ServerResponse<PageInfo> selectByParam(Map param){
        PageHelper.startPage(NumberUtil.safeToNumber(param.get("pageNo"),1),
                NumberUtil.safeToNumber(param.get("pageSize"),10),StringUtil.valueOfString(param.get("orderBy"),"id desc"));
        List<Map> resultList = permissionMapper.selectByParam(StringUtil.valueOfString(param.get("names")));
        PageInfo pageInfo = new PageInfo(resultList);
        pageInfo.setList(resultList);
        return ServerResponse.createBySuccess("查询成功",pageInfo);
    }

}
